package ru.budget.mobile.ui.primary

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.zIndex
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.launch
import org.koin.androidx.compose.koinViewModel
import ru.budget.mobile.R
import ru.budget.mobile.ui.base.BudgetViewModel
import ru.budget.mobile.ui.components.ActionMenu
import ru.budget.mobile.ui.components.TextFAB
import ru.budget.mobile.ui.consumption.BottomSheet
import ru.budget.mobile.ui.consumption.currentSelect
import ru.budget.mobile.ui.entities.ConsumptionUI
import ru.budget.mobile.ui.entities.IncomeUI
import ru.budget.mobile.ui.entities.TransactionType
import ru.budget.mobile.ui.main.visiblyActionMenu
import ru.budget.mobile.ui.navigation.BottomNavigation
import ru.budget.mobile.ui.navigation.NavigationGraph

@Composable
@Preview(
    showBackground = true,
    device = "id:Xiaomi POCO M5s"
)
private fun PrimaryScreenPreview() = PrimaryScreen()

private val show = mutableStateOf(false)
private val type = mutableStateOf(TransactionType.CONSUMPTION)

private val budgetViewModel @Composable get() = koinViewModel<BudgetViewModel>()

@OptIn(ExperimentalMaterial3Api::class)
private val sheetState @Composable get() = rememberModalBottomSheetState()

@Composable
fun PrimaryScreen() {
    val navigationController = rememberNavController()

    Scaffold(
        bottomBar = { BottomNavigation(navigation = navigationController) },
        content = {
            BackgroundActionMenu()
            Column(Modifier.padding(it)) { NavigationGraph(controller = navigationController) }
        },
        floatingActionButton = { ActionMenu() }
    )
}

@Composable
private fun BackgroundActionMenu() =
    if (visiblyActionMenu.value) {
        Box(
            modifier = Modifier
                .background(Color(0x4D000000))
                .fillMaxSize()
                .zIndex(1f)
                .clickable(
                    onClick = { visiblyActionMenu.value = false },
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() }
                ),
            content = { BottomSheet() }
        )
    } else null

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun BottomSheet() =
    if (show.value) {
        val viewModel = budgetViewModel
        BottomSheet(
            sheetState = sheetState,
            type = type.value,
            back = { hideBottomSheet() },
            save = {
                if (type.value == TransactionType.INCOME) {
                    viewModel.addIncome(
                        income = IncomeUI(
                            sum = it.sum,
                            datetime = it.datetime
                        )
                    )
                } else {
                    viewModel.addConsumption(
                        consumption = ConsumptionUI(
                            title = it.title,
                            type = it.type,
                            category = it.category,
                            sum = it.sum,
                            datetime = it.datetime
                        )
                    )
                }
                hideBottomSheet()
            },
            onDismissRequest = { hideBottomSheet() }
        )
    } else null

private fun hideBottomSheet() {
    currentSelect.intValue = -1
    show.value = false
}

private fun showBottomSheet(transactionType: TransactionType) {
    type.value = transactionType
    show.value = true
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun StatesListener() {
    val sheetState = sheetState

    LaunchedEffect(key1 = Unit) {
        launch {
            if (show.value) {
                when (type.value) {
                    TransactionType.CONSUMPTION -> sheetState.show()

                    TransactionType.INCOME -> sheetState.show()
                }
            } else {
                when (type.value) {
                    TransactionType.CONSUMPTION -> sheetState.hide()

                    TransactionType.INCOME -> sheetState.hide()
                }
            }
        }
    }
}

@Composable
private fun ActionMenu() {
    StatesListener()
    ActionMenu(
        visibly = visiblyActionMenu.value,
        items = {
            TextFAB(
                title = "Расход",
                icon = R.drawable.minus,
                onClick = { showBottomSheet(TransactionType.CONSUMPTION) }
            )
            TextFAB(
                title = "Доход",
                icon = R.drawable.add,
                onClick = { showBottomSheet(TransactionType.INCOME) }
            )
            TextFAB(
                title = "Закрыть",
                icon = R.drawable.close,
                onClick = { visiblyActionMenu.value = false }
            )
        }
    )
}
